package com.qiaoya.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.UUID;


public class UuidUtil {

	public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
			"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8",
			"9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
			"U", "V", "W", "X", "Y", "Z" };
	//C#转Java算法
	//private static char[] strServerKey = {'0','L'};//交易流水
		//private static char[] strWxUserIdKey = {'A','4'};
	    /// <summary>
	    /// 序列号
	    /// </summary>
	   // private static UInt32 nCurrentSeq = 0;
		private static int nCurrentSeq = 0;
	    /// <summary>
	    /// 起始时间毫秒数
	    /// </summary>
	    private static long lPreTime = -1;
	    /// <summary>
	    /// 字符索引
	    /// </summary>
	    private static char[] chIds = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
	    /// <summary>
	    /// 类锁
	    /// </summary>
	    private static Object objLock = new Object();
	    /// <summary>
	    /// 格林威治起始时间
	    /// </summary>
	   // private static DateTime dtGreater = DateTime.Parse("01/01/1970");
	    //private static DateTime dtGreater = DateTime.Parse("01/01/1970");
	    /// <summary>
	    /// 生成用户ID
	    /// </summary>
	    /// <param name="chId">传入16位字符数组</param>
	    /// <returns>填充传入的数组的后12位</returns>

	public static String get32UUID() {
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		return uuid;
	}
   //添加UUID算法
	public static String get16UUID() {
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 16; i++) {
			String str = uuid.substring(i * 2, i * 2 + 2);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		return shortBuffer.toString();
	}
	
	public static String get8UUID() {
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		return shortBuffer.toString();

	}
	
	public static int MakeId(char[] strServerKey,char[] strWxUserIdKey, char[] chId)
    {
        chId[0] = strWxUserIdKey[0];
        chId[1] = strWxUserIdKey[1];
        chId[14] = strServerKey[0];
        chId[15] = strServerKey[1];

        long t = 0;
        
        int s = 0;
        synchronized(objLock)
        {
        	/*String time = "2017-04-14 18:49:00";
            Date date=null;  
            SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
            try {
				date=formatter.parse(time);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} */ 
            t = System.currentTimeMillis();
            if (t == lPreTime)
            {
                nCurrentSeq++;
            }//
            else
            {
                lPreTime = t;
                nCurrentSeq = 0;
            }
            s = nCurrentSeq;
        }
        for (int i = 10; i >= 2; i--)
        {
        	long ti = t & 0x1F;
            t >>= 5;
            chId[i] = chIds[(int)ti];
        }
        for (int i = 13; i >= 11; i--)
        {
            int si = s & 0x1F;
            s >>= 5;
            chId[i] = chIds[si];
        }
        for(char char1: chId){
        	System.out.println(char1);
        }
        
        return 0;
    }

	//获取ID
    public static String MakeId()
    {
        char[] chId = new char[16];
        char[] strServerKey = {'0','Z'};
        char[] strWxUserIdKey = {'A','4'};
       // MakeId(chId);
        MakeId(strServerKey,strWxUserIdKey,chId);
        return new String(chId);
    }
    
	//获取地址ID
    public static String MakeAddresId()
    {
        char[] chId = new char[16];
        char[] strServerKey = {'0','Z'};
        char[] strWxUserIdKey = {'B','4'};
       // MakeId(chId);
        MakeId(strServerKey,strWxUserIdKey,chId);
        return new String(chId);
    }
    
	//获取银行卡ID
    public static String MakeBankId()
    {
        char[] chId = new char[16];
        char[] strServerKey = {'0','Z'};
        char[] strWxUserIdKey = {'B','1'};
       // MakeId(chId);
        MakeId(strServerKey,strWxUserIdKey,chId);
        return new String(chId);
    }
    
    //获取流水ID
    public static String makeDetId()
    {
        char[] chId = new char[16];
        char[] strServerKey = {'0','Z'};
        char[] strWxUserIdKey = {'A','4'};
       // MakeId(chId);
        MakeId(strServerKey,strWxUserIdKey,chId);
        return new String(chId);
    }
    
  //获取订单ID
    public static String makeOrdersId()
    {
        char[] chId = new char[16];
        char[] strServerKey = {'0','Z'};
        char[] strWxUserIdKey = {'A','9'};
       // MakeId(chId);
        MakeId(strServerKey,strWxUserIdKey,chId);
        return new String(chId);
    }
  //获取用户ID
    public static String makeUserId()
    {
//    	用户基本信息ID：前两位为业务ID（A1为用户，A2为服务商，A3为运营商，A4为运营商片区，A5为电子交易流水，
//    	A6为红包交易明细，A7为会计账户，务项目规格B1为自营服务项目，B2为服务市场服务项目，B3为自营服务项目规格，
//    	B4为服务市场服，C1优惠券编码，D1为订单编号），中间两位为机器ID，后12位为自动生成ID
        char[] chId = new char[16];
        char[] strServerKey = {'0','Z'};
        char[] strWxUserIdKey = {'A','2'};
       // MakeId(chId);
        MakeId(strServerKey,strWxUserIdKey,chId);
        return new String(chId);
    }
	

//	public static void main(String[] args) {
//		System.out.println(makeDetId().toString());
//		System.out.println(makeUserId().toString());
//          //数据库连接测试
//		/*  String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
//		  String dbURL="jdbc:sqlserver://192.168.1.115:1433;DatabaseName=qytest0411-2";
//		  String userName="test";
//		  String userPwd="123456";
//		  try
//		  {
//		   Class.forName(driverName);
//		   Connection dbConn= DriverManager.getConnection(dbURL,userName,userPwd);
//		    System.out.println("连接数据库成功");
//		  }
//		  catch(Exception e)
//		  {
//		   e.printStackTrace();
//		   System.out.print("连接失败");
//		  }   */ 
//	}
}
